﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PolePosition.v3.Desktop.Editors
{
    public partial class EditWorkQueueItem : DialogBase, IDialogBase
    {
        public EditWorkQueueItem(bool IsEdit, object data)
        {
            InitializeComponent();

            m_IsEdit = IsEdit;
            Data = data;

            if (IsEdit)
            {
                base.buttonXOk.Text = "Save";
                base.ShowProgressPanel = true;
            }
            else
            {
                base.buttonXOk.Text = "Ok";
                base.ShowProgressPanel = false;
            }

            remedy1.EditAllowed = IsEdit;
            remedy1.Data = data;

            if (data.GetType() == typeof(PolePosition.v3.EntityLibrary.UtilityAssetXMissionStructurePolesCrossRemedyVendorInspectionVisitEntity))
            {
                DataType = Common.Enumerations.DataTypes.WorkQueueXmission;
                m_XmissionRemedy = data as EntityLibrary.UtilityAssetXMissionStructurePolesCrossRemedyVendorInspectionVisitEntity;
            }
            else if (data.GetType() == typeof(PolePosition.v3.EntityLibrary.VendorDistributionPoleRemedyEntity))
            {
                DataType = Common.Enumerations.DataTypes.WorkQueueDistribution;
                m_DistributionRemedy = data as EntityLibrary.VendorDistributionPoleRemedyEntity;
            }
        }

        private EntityLibrary.UtilityAssetXMissionStructurePolesCrossRemedyVendorInspectionVisitEntity m_XmissionRemedy;
        private EntityLibrary.VendorDistributionPoleRemedyEntity m_DistributionRemedy;

        public Common.Enumerations.DataTypes DataType { get; private set; }

        private bool m_IsEdit;

        public object Data { get; set; }

        #region Asynch Processing (Implements IDialogBase)

        //to start the asynch background worker call - base.StartAsynchWork()

        public override void DoAsynchWork(DoWorkEventArgs e)
        {
            //do whatever needs to be done asynch here (BackgroudWorker_DoWork)
            if (!m_IsEdit)
                return;

            if (DataType == Common.Enumerations.DataTypes.WorkQueueXmission)
            {
                m_XmissionRemedy = ServiceProxy.UtilityService.SaveXmissionWorkqueue(m_XmissionRemedy);
                Data = m_XmissionRemedy;
            }
            else if (DataType == Common.Enumerations.DataTypes.WorkQueueDistribution)
            {
                m_DistributionRemedy = ServiceProxy.UtilityService.SaveDistributionWorkqueue(m_DistributionRemedy);
                Data = m_DistributionRemedy;
            }
        }

        public override void AsychWorkComplete(object sender, RunWorkerCompletedEventArgs e)
        {
            base.AsychWorkComplete(sender, e);
            //do whatever needs to be done after asynch work complete (BackgroudWorker_RunWorkerCompleted)

            if (e.Error != null)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Error saving work queue data.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            
            this.DialogResult = DialogResult.OK;
        }

        #endregion
        
        private void buttonXOk_Click(object sender, EventArgs e)
        {
            if (!m_IsEdit)
            {
                this.DialogResult = DialogResult.Cancel;
                return;
            }

            if (DataType == PolePosition.v3.Common.Enumerations.DataTypes.WorkQueueXmission)
            {
                if (!m_XmissionRemedy.Utility_Asset_XMissionStructure_Pole_CrossRemedy_Vendor_InspectionVisitId.HasValue)
                    m_XmissionRemedy.IsNew = true;

                if (m_XmissionRemedy.IsDirty.Value || m_XmissionRemedy.IsNew.Value)
                    StartAsynchWork();
                else
                    this.DialogResult = DialogResult.Cancel;
            
            }
            else if (DataType == PolePosition.v3.Common.Enumerations.DataTypes.WorkQueueDistribution)
            {
                if (!m_DistributionRemedy.RemedyId.HasValue)
                    m_DistributionRemedy.IsNew = true;

                if (m_DistributionRemedy.IsDirty.Value || m_DistributionRemedy.IsNew.Value)
                    StartAsynchWork();
                else
                    this.DialogResult = DialogResult.Cancel;

            }
        }
    }
}
